package phone.qyg.com.qyg.activity;

import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.net.http.SslError;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.content.ContextCompat;
import android.view.View;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;
import android.widget.Toast;

import com.app.base.tool.BaseActivity;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;

/**
 * 直播
 */
@ContentView(R.layout.activity_video)
public class VideoActivity extends BaseActivity {
    @ViewInject(R.id.back)
    TextView back;


    @ViewInject(R.id.webView)
    WebView webView;

    @ViewInject(R.id.tab)
    TabLayout tab;
    ProgressDialog dialog;
    String url1="https://shangzhibo.tv/watch/8432915";
    String url2="https://shangzhibo.tv/watch/4873219";
    String url3="https://shangzhibo.tv/watch/4487109";
    String url4="https://shangzhibo.tv/watch/5936164";
    String url5="https://shangzhibo.tv/watch/2512367";
    @Override
    public void initSucessView() {

    }

    @Override
    public void initData(Bundle savedInstanceState) {
        dialog = new ProgressDialog(this);
        dialog.show();
        dialog.setMessage("加载中...");

        tab.addTab(tab.newTab().setText("直播一"));
        tab.addTab(tab.newTab().setText("录播一"));
        tab.addTab(tab.newTab().setText("录播二"));
        tab.addTab(tab.newTab().setText("录播三"));
        tab.addTab(tab.newTab().setText("录播四"));
        tab.setTabMode(TabLayout.MODE_SCROLLABLE);
        tab.setTabTextColors(ContextCompat.getColor(this, R.color.black), ContextCompat.getColor(this, R.color.home_title_line));
        tab.setSelectedTabIndicatorColor(ContextCompat.getColor(this, R.color.home_title_line));

        WebSettings ws = webView.getSettings();
        ws.setBuiltInZoomControls(true);// 隐藏缩放按钮
        ws.setUseWideViewPort(true);// 可任意比例缩放
        ws.setLoadWithOverviewMode(true);// setUseWideViewPort方法设置webview推荐使用的窗口。setLoadWithOverviewMode方法是设置webview加载的页面的模式。

        ws.setSavePassword(true);
        ws.setSaveFormData(true);// 保存表单数据
        ws.setJavaScriptEnabled(true);

        ws.setDomStorageEnabled(true);
        ws.setSupportMultipleWindows(true);// 新加

        //这行很关键
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                dialog.dismiss();
            }

            @Override
            public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
                super.onReceivedError(view, request, error);
                dialog.dismiss();
                Toast.makeText(mContext,"连接缓慢,稍后再试",Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
                handler.proceed();
                dialog.dismiss();
            }
        });

        webView.loadUrl(url1);

        tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if(dialog.isShowing()){
                    dialog.dismiss();
                }
                dialog.show();
                switch (tab.getPosition()){
                    case 0:
                        webView.loadUrl(url1);
                        break;
                    case 1:
                        webView.loadUrl(url2);
                        break;
                    case 2:
                        webView.loadUrl(url3);
                        break;
                    case 3:
                        webView.loadUrl(url4);
                        break;
                    case 4:
                        webView.loadUrl(url5);
                        break;
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }


    @Override
    public void initViewHandler() {

    }
    @Event(value = {R.id.back})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                finish();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(webView!=null){
            webView.destroy();
        }
    }
}
